import {guid} from '../util/guid.js'
import message from './message_pb'

/**
 * 创建 google protocol buffer 协议消息实列
 */
class MessageFactory {

    static MsgType = {
        'TEXT': 0,
        'IMAGE': 1,
        'VOICE': 2,
        'MEDIA': 3,
        'CONNECT': 4,
        'KEEPALIVE': 5,
        'FRIEND': 6,
        'SIGNED': 7
    }

    static ChatType = {
        'SINGLE': 0,
        'GROUP': 1
    }


    /**
     *
     * @param fromId 发送人ID
     * @param toId  接收人ID
     * @param content 内容
     * @param type 消息类型
     * @param chatType 聊天类型
     */
    static createMsg(fromId, toId, content, type, chatType,groupIds) {
        let time = new Date().getTime()
        let msg = new message.Message()
        msg.setGuid(guid())
        msg.setFromid(fromId)
        msg.setToid(toId)
        msg.setContent(content)
        msg.setTime(time)
        msg.setType(type)
        msg.setChattype(chatType)
        msg.setGroupidsList(groupIds)
        return msg;
    }

    /**
     * 发送上线信息
     * @param userId 当前用户ID
     * @param content 消息内容
     */
    static createConnectMsg(userId, groupIds, content) {
        return MessageFactory.createMsg(userId, null, content, MessageFactory.MsgType.CONNECT, null,groupIds)
    }


    /**
     * 心跳消息
     * @returns {message.Message}
     */
    static createHeartMsg() {
        return MessageFactory.createMsg(null, null, 'I am alive', MessageFactory.MsgType.KEEPALIVE, null)
    }




    /**
     * 发送文本消息
     * @param fromId
     * @param toId
     * @param content
     * @param type
     * @param chatType
     */
    static createTextMsg(fromId, toId, content, chatType) {
        return MessageFactory.createMsg(fromId, toId, content, MessageFactory.MsgType.TEXT, chatType)
    }


    /**
     * 发送图片消息
     * @param fromId
     * @param toId
     * @param content
     * @param type
     * @param chatType
     */
    static createImageMsg(fromId, toId, content, type, chatType) {
        return MessageFactory.createMsg(fromId, toId, content, MessageFactory.MsgType.IMAGE, chatType)
    }

    /**
     * 获取跟当前用户聊天的人
     * @param msgObject
     */
    static getMsgKey(msgObject,uid){

    }

    /**
     * 反序列化 将收到的序列化消息反序列化
     * @param bytes
     * @returns {*}
     */
    static deserializeBinary(bytes){
       return message.Message.deserializeBinary(bytes);
    }

    /**
     * 将消息序列化
     * @param protoMsg
     * @returns {*}
     */
    static serializeBinary(protoMsg){
        return protoMsg.serializeBinary();
    }

}

export default MessageFactory